﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections.Specialized;
using System.Data.Linq;
using Ample.DAL.User;
using Ample.Model;


namespace Ample.DAL
{
    public class UserManager : DBBase
    {
        public static UserManager Create()
        {
            return new UserManager();
        }

        #region Select Methods

        public UserProfile GetUserProfileByUsername(string username)
        {
            using (AmpleDataContext context = DataContext)
            {
                var v = from c in context.UserProfiles where c.User.UserName == username select c;
                
                return v.SingleOrDefault();
            }
        }

        public Model.User GetUserByUsername(string username)
        {
            using (AmpleDataContext context = DataContext)
            {
                context.LoadOptions = UserQueries.UserOptions;
                return UserQueries.compiledQuery_GetUserByUsername(context, username).SingleOrDefault();
            }
        }

        #endregion

        #region Update Methods

        public OperationStatus UpdateUserProfile(UserProfile userProfile)
        {
            AmpleDataContext context = null;
            try
            {
                context = DataContext;

                context.UserProfiles.Attach(userProfile);
                
                context.SubmitChanges();
            }
            catch (Exception exp)
            {
                return new OperationStatus(false, exp.Message, exp);
            }
            finally
            {
                if (context != null) context.Dispose();
            }
            return new OperationStatus(true, null, null);
        }

        #endregion

        #region Insert Methods

        public OperationStatus InsertUserProfile(UserProfile userProfile)
        {
            AmpleDataContext context = null;
            try
            {
                context = DataContext;

                context.UserProfiles.InsertOnSubmit(userProfile);

                context.SubmitChanges();
            }
            catch (Exception exp)
            {
                return new OperationStatus(false, exp.Message, exp);
            }
            finally
            {
                if (context != null) context.Dispose();
            }
            return new OperationStatus(true, null, null);
        }

        #endregion

        #region Save Methods

        public OperationStatus SaveUserProfile(UserProfile userPro)
        {
            AmpleDataContext context = null;

            try
            {
                context = DataContext;
                
                //see if the userprofile is in the system
                Model.UserProfile dbUserProfile =
                    context.UserProfiles.Where(x => x.UserId == userPro.UserId).SingleOrDefault();
                bool isNew = false;
                if (dbUserProfile == null)
                {
                    dbUserProfile = new Model.UserProfile();
                    isNew = true;
                }
                //else
                //{
                //    //remove them for refresh
                //    //wish there was a better way to do this but...
                //    db.ProductDescriptors.DeleteAllOnSubmit(from pd in db.ProductDescriptors where pd.ProductID == product.ID select pd);
                //}

                //add the descriptors
                //foreach (ProductDescriptor pd in product.Descriptors)
                //{
                //    Commerce.Data.SqlRepository.ProductDescriptor dbPd = new Commerce.Data.SqlRepository.ProductDescriptor();
                //    dbPd.ProductID = product.ID;
                //    dbPd.Title = pd.Title;
                //    dbPd.Body = pd.Body;
                //    dbProduct.ProductDescriptors.Add(dbPd);
                //}

                //some left/right
                dbUserProfile.UserId = userPro.UserId;
                dbUserProfile.Country = userPro.Country;
                dbUserProfile.SponsorID = userPro.SponsorID;
                dbUserProfile.SponsorName = userPro.SponsorName;


                if (isNew)
                    context.UserProfiles.InsertOnSubmit(dbUserProfile);


                context.SubmitChanges();
            }
            catch (Exception exp)
            {
                return new OperationStatus(false, exp.Message, exp);
            }
            finally
            {
                if (context != null) context.Dispose();
            }
            return new OperationStatus(true, null, null);
            
        }

        #endregion

    }
}
